<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>

    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .con {
        position: relative;
        margin: 30px auto;
        width: 800px;
        height: 400px;
        background-color: pink;
      }
      .left {
        position: absolute;
        top: 50%;
        left: 0px;
        margin-top: -30px;
        width: 40px;
        height: 60px;
        font-size: 50px;
        color: #fff;
        background-color: black;
        opacity: 0.6;
        border: none;
        cursor: pointer;
      }
      .left:hover {
        background-color: #fff;
        color: black;
      }
      .right {
        position: absolute;
        top: 50%;
        right: 0px;
        margin-top: -30px;
        width: 40px;
        height: 60px;
        color: #fff;
        font-size: 50px;
        opacity: 0.6;
        background-color: black;
        border: none;
        cursor: pointer;
      }
      .right:hover {
        background-color: #fff;
        color: black;
      }
      img {
        width: 800px;
        height: 400px;
      }
    </style>
  </head>
  <body>
    <div class="con">
      <img class="tup" src="../imges/2.1.jpg" alt="" />
      <button class="left"><</button>
      <button class="right">></button>
    </div>
    <script>
      var img = [
        { imgSrc: "../imges/2.1.jpg" },
        { imgSrc: "../imges/2.2.jpg" },
        { imgSrc: "../imges/2.3.jpg" },
        { imgSrc: "../imges/2.4.jpg" },
        { imgSrc: "../imges/2.5.jpg" },
      ];
      var tu = document.querySelector(".tup");
      var leftBtn = document.querySelector(".left");
      var rightBtn = document.querySelector(".right");

      var i = 0;
      var time;
      function fun() {
        time = setInterval(function () {
          i++;
          // 修改src
          tu.src = img[i].imgSrc;
          if (i === img.length - 1) {
            i = -1;
          }
        }, 2000);
      }
      fun();
      //   tu.addEventListener("mo");
      // 右侧按钮
      rightBtn.addEventListener("click", function () {
        i++;
        tu.src = img[i].imgSrc;
        if (i === img.length - 1) {
          i = -1;
        }
      });
      // 左侧按钮
      leftBtn.addEventListener("click", function () {
        if (i < 0) {
          i = img.length - 1;
        }
        tu.src = img[i].imgSrc;
        i--;
      });
      tu.addEventListener("mouseenter", function () {
        clearInterval(time);
      });
      tu.addEventListener("mouseleave", fun);
      leftBtn.addEventListener("mouseenter", function () {
        clearInterval(time);
      });
      leftBtn.addEventListener("mouseleave", fun);
      rightBtn.addEventListener("mouseenter", function () {
        clearInterval(time);
      });
      rightBtn.addEventListener("mouseleave", fun);
    </script>
  </body>
</html>
